package com.example.myapplication.utils;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

public class Preference {

    private final static String KEY_WORD_DB = "KEY_WORD_DB";
    private final static String KEY_START_IDX = "KEY_START_IDX";
    private final static String KEY_END_IDX = "KEY_END_IDX";
    private final static String KEY_ORDER_IDX = "KEY_ORDER_IDX";

    private SharedPreferences cache;
    private static Preference instance;//单例模式

    private Preference(Context ctx) {
        cache = ctx.getSharedPreferences("local", Context.MODE_PRIVATE);
    }

    public static Preference getInstance(Context ctx) {
        if (instance == null) {
            instance = new Preference(ctx);
        }
        return instance;
    }

    public void setWordDB(String dbStr) {
        cache.edit().putString(KEY_WORD_DB, dbStr).apply();
    }

    public String getWordDb() {
        return cache.getString(KEY_WORD_DB, "CET6");
    }

    public void setStartIdx(int idx) {
        cache.edit().putInt(KEY_START_IDX, idx).apply();
    }
    public int getStartIdx() {
        return cache.getInt(KEY_START_IDX, 0);
    }

    public void setEndIdx(int idx) {
        cache.edit().putInt(KEY_END_IDX, idx).apply();
    }
    public int getEndIdx() {
        return cache.getInt(KEY_END_IDX, 0);
    }

    public void setOrder(int i) {
        cache.edit().putInt(KEY_ORDER_IDX, i).apply();
    }

    public int getOrder() {
        return cache.getInt(KEY_ORDER_IDX, 0);
    }
}